package com.gfxy.modules.dbcenter.service.impl;

import com.gfxy.modules.dbcenter.entity.Department;
import com.gfxy.modules.dbcenter.entity.Teacher;
import com.gfxy.modules.dbcenter.service.IDepartmentService;
import com.gfxy.modules.dbcenter.service.IGfxyDataSynchronismService;
import com.gfxy.modules.dbcenter.service.ITeacherDataSynchronismService;
import com.gfxy.modules.system.entity.SysDepart;
import com.gfxy.modules.system.entity.SysUser;
import com.gfxy.modules.system.service.ISysDepartService;
import com.gfxy.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: lijixin 2021/6/10 8:48 上午
 */
@Slf4j
@Service
public class GfxyDataSynchronismServiceImpl implements IGfxyDataSynchronismService {
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private ITeacherDataSynchronismService teacherDataSynchronismService;

    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private ISysUserService sysUserService;




    @Override
    public void synchronismDepartment(String userName) {
        List<Department> sourceResults = departmentService.getAllDepartment();
        System.out.println(sourceResults);

        List<SysDepart> sysDeparts = new ArrayList<>();

        SysDepart sysDepart1 = new SysDepart();
        sysDepart1.setParentId("A05");
        sysDepart1.setId("0");
        sysDepart1.setDepartName("校领导");
        sysDepart1.setOrgCode("0");
        sysDepart1.setOrgType("-1");
        sysDepart1.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
        sysDeparts.add(sysDepart1);

        SysDepart sysDepart2 = new SysDepart();
        sysDepart2.setParentId("A05");
        sysDepart2.setId("1");
        sysDepart2.setDepartName("党务部门");
        sysDepart2.setOrgCode("1");
        sysDepart2.setOrgType("-1");
        sysDepart2.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
        sysDeparts.add(sysDepart2);

        SysDepart sysDepart3 = new SysDepart();
        sysDepart3.setParentId("A05");
        sysDepart3.setId("2");
        sysDepart3.setDepartName("行政部门");
        sysDepart3.setOrgCode("2");
        sysDepart3.setOrgType("-1");
        sysDepart3.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
        sysDeparts.add(sysDepart3);

        SysDepart sysDepart4 = new SysDepart();
        sysDepart4.setParentId("A05");
        sysDepart4.setId("3");
        sysDepart4.setDepartName("教学部门");
        sysDepart4.setOrgCode("3");
        sysDepart4.setOrgType("-1");
        sysDepart4.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
        sysDeparts.add(sysDepart4);


        System.out.println(sourceResults);
        for (Department sourceResult : sourceResults) {
            SysDepart sysDepart = new SysDepart();
            sysDepart.setParentId(sourceResult.getBmid().substring(0,1));
            sysDepart.setId(sourceResult.getBmid());
            sysDepart.setDepartName(sourceResult.getBmmc());

            sysDepart.setCreateBy(userName);
            sysDepart.setOrgCode(sourceResult.getBmid());

            sysDepart.setOrgType(this.translateOrgType(sourceResult.getDwlx()));
            sysDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
            sysDeparts.add(sysDepart);
        }
        System.out.println(sysDeparts);
        sysDepartService.saveBatch(sysDeparts);
    }

    private String translateOrgType(String dwlx) {
        if(dwlx.trim().equals("校领导")){
            return "0";
        } else if(dwlx.trim().equals("党务部门")){
            return "1";
        } else if(dwlx.trim().equals("行政部门")){
            return "2";
        } else if(dwlx.trim().equals("教学部门")){
            return "3";
        }
        return null;
    }

    @Override
    public void synchronismTeacher(String userName) {
        List<Teacher> teacherSource = teacherDataSynchronismService.getAllTeacherInfo();
        log.debug("从数据中心获取 {} 条数据", teacherSource.size());
        List<SysUser> addSysUsers = new ArrayList<>();
        List<SysUser> updateSysUsers = new ArrayList<>();
        Map<String,SysUser> currentSysUsers = this.getCurrentSysUsers();
        for (Teacher teacher : teacherSource) {
            if(currentSysUsers.containsKey(teacher.getGh())){
                SysUser sysUser = currentSysUsers.get(teacher.getGh());
                sysUser.setDepartIds(teacher.getDwh());
                sysUser.setUpdateBy(userName);
            } else {
                SysUser user = new SysUser();
                user.setUsername(teacher.getGh());
                user.setRealname(teacher.getXm());
                user.setCreateTime(new Date());//设置创建时间
                String salt = oConvertUtils.randomGen(8);
                user.setSalt(salt);
                //todo
                String passwordEncode = PasswordUtil.encrypt(user.getUsername(), "111111", salt);
                user.setPassword(passwordEncode);
                //todo birthday
                if(!oConvertUtils.isEmpty(teacher.getXb())){
                    user.setSex(teacher.getXb().equals("男")? 1:2);
                }
                user.setWorkNo(teacher.getGh());
                user.setStatus(1);
                user.setDelFlag(CommonConstant.DEL_FLAG_0);
                user.setUpdateBy(userName);
                user.setUpdateTime(new Date());
                sysUserService.addUserWithRole(user, "1402832775044915202");
                sysUserService.addUserWithDepart(user, teacher.getDwh());
                addSysUsers.add(user);
            }
        }
        sysUserService.updateBatchById(updateSysUsers);
        sysUserService.saveBatch(addSysUsers);
    }

    private Map<String, SysUser> getCurrentSysUsers() {

        List<SysUser> sysUserList = sysUserService.list();
        Map<String,SysUser> result = new HashMap<>(sysUserList.size());
        for (SysUser sysUser : sysUserList) {
            result.put(sysUser.getWorkNo(), sysUser);
        }
        return result;
    }

    public static void main(String[] args) {
        String str = "123";
        System.out.println(str.substring(0,1));
    }
}
